package com.mydomain.Bodega;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;

@Stateful
@Name("egresoPorTransferencia")
@Scope(ScopeType.EVENT)
public class EgresoPorTransferencia implements  IEgresoPorTransferencia{
	@In(required=false,value="#{invEgresoHome.instance.egrCodigo}")
	private long egrCodigo; 
	
	
	private BigDecimal varTotalEgresoNeto= new BigDecimal(0.0);
	
	@PersistenceContext
	EntityManager em;

	@Factory("egresoItemsPorTransferencia")
	public List<InvEgresoItem> egresoItemsPorTransferencia()
	{
		if (this.egrCodigo>0)
	 		return em.createQuery("SELECT i FROM  InvEgresoItem  i JOIN i.invEgreso s " +
	 				" where  s.egrCodigo = :egreso ").setParameter("egreso", this.egrCodigo).getResultList(); 
	 else
		 return null;
		
	}
		
	public BigDecimal getVarTotalEgresoNeto() {
		 List<InvEgresoItem> listado=egresoItemsPorTransferencia();
		for (InvEgresoItem invEgresoItem : listado) {
			varTotalEgresoNeto=varTotalEgresoNeto.add(invEgresoItem.getEgiPrecioU().multiply(invEgresoItem.getEgiCantidad()));
			
		}
		return varTotalEgresoNeto.setScale(
				2,
				varTotalEgresoNeto.ROUND_HALF_UP);
	}

	@Remove
	@Destroy
	public void destroy() {
	}
}
